/*(C) Copyright

    This file is part of widget Meteoclimatic.
    Autor: Antonio Cristóbal Álvarez Abellán -> acabellan@gmail.com
    
    */

package deeloco.android.meteoclimatic.donate;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import deeloco.android.meteoclimatic.donate.datos.ViewPagerAdapter;

//import com.mobaloo.mobadme.MobadmeLibActivity;
//import com.mobaloo.mobadme.MobadmeLibActivity.BackProcess;

public class tabPantalla extends SherlockFragmentActivity implements OnPageChangeListener, TabListener {
	
//	MobadmeLibActivity mla;

	private String[] locations;
    private ViewPager pager;
    private String titulo;
    Bundle extras;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    extras = getIntent().getExtras();
    	titulo = extras.getString("nombre");
	    
//	    ActionBar actionBar = getSupportActionBar();
//        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//        actionBar.setDisplayShowTitleEnabled(true);
//        
//        if (actionBar != null)
//        {
//        	Bundle extras = getIntent().getExtras();
//        	String nombreEstacion = extras.getString("nombre");
//            actionBar.setTitle(nombreEstacion);
//            
//            //actionBar.setIcon(FunGlobales.resize(getResources(), getResources().getDrawable(R.drawable.icon), 32, 32));
//        };
// 
//        Tab tab = actionBar.newTab()
//                    .setText("Datos Estacion")
//                    .setTabListener(new datosEstacion())
//                    .setIcon(android.R.drawable.ic_menu_gallery);
// 
//        actionBar.addTab(tab);
// 
//        tab = actionBar.newTab()
//                    .setText("Previsión")
//                    .setTabListener(new prevision())
//                    .setIcon(android.R.drawable.ic_menu_edit);
// 
//        actionBar.addTab(tab);
//	    
        setContentView(R.layout.activity_main);
        locations = getResources().getStringArray(R.array.locations);
        pager = ((ViewPager) findViewById(R.id.pager));
        configureViewPager();
        configureActionBar();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
//		ImageView iv = (ImageView) this.findViewById(R.id.ImageView);
//		mla.flagRefresh=0;
		//BackProcess b = mla.bp("ameteoclimatic/home", this,iv,0,0);
//		BackProcess b = mla.bp("agastosmovil/home", this,iv,0,0);
		
//		b.execute();

	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
//		mla.flagRefresh = 1;
//		if (mla.timer!=null){
//		mla.timer.cancel();
		}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
	{
		getSupportMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
 
        super.onOptionsItemSelected(item);
 
        switch(item.getItemId()){
            case R.id.informacionEstacion:
                //Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
            	
                Intent informacionEstacion = new Intent(getBaseContext(), InformacionEstacion.class);
                informacionEstacion.putExtra("codigo", extras.getString("codigo"));
                informacionEstacion.putExtra("nombre", extras.getString("nombre"));
                informacionEstacion.putExtra("estado", extras.getBoolean("estado"));
                informacionEstacion.putExtra("desEstado", extras.getString("desEstado"));
                startActivity(informacionEstacion);
                break;
        }
        return true;
 
    }
	
	
    private void configureViewPager() 
    {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), locations,extras);
        pager.setAdapter(viewPagerAdapter);
        pager.setOnPageChangeListener(this);
    }
    
    
    private void configureActionBar() 
    {
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        getSupportActionBar().setTitle(titulo);
        for (String location: locations) {
            Tab tab = getSupportActionBar().newTab();
            tab.setText(location);
            tab.setTabListener(this);
            getSupportActionBar().addTab(tab);
        }
    }
    
    
    public void onPageSelected(int position) 
    {
        Tab tab = getSupportActionBar().getTabAt(position);
        getSupportActionBar().selectTab(tab);
    }
    
    
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        int position = tab.getPosition();
        pager.setCurrentItem(position);
    }
    
    public void onPageScrollStateChanged(int position) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}
	
	
	}
	
	

//}

